package com.ruoyi.common.utils;

import com.ruoyi.common.core.text.Convert;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class CamelUtil {
    public static DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static Map toUnderline(Map<String, Object> inData) {
        Map<String, Object> outData = new HashMap<String, Object>();

        inData.forEach((key, value) -> {
            String _key = StringUtils.toUnderScoreCase(key);
            outData.put(_key, value);
        });

        return outData;
    }

    public static void main(String[] args) {
        Map map = new HashMap();
        map.put("createId", 1);
        map.put("createTime", new Date());

        System.out.println(toUnderline(map));
    }

    public static Map<String, Object> toCameCale(Map<String, Object> data) {
        Map<String, Object> outData = new HashMap<String, Object>();

        data.forEach((key, value) -> {
            String _key = StringUtils.toCamelCase(key);
            outData.put(_key, value);
        });

        return outData;
    }

    public static List<?> toCameCaleList(List<Map> list) {
        List<Map<String, Object>> outList = new ArrayList<Map<String, Object>>();

        list.forEach(map -> {
            outList.add(toCameCale(map));
        });

        return outList;
    }

    public static void formatTime(Map map) {
        Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            String key = Convert.toStr(entry.getKey());
            Object value = entry.getValue();

            if (value != null && value instanceof LocalDateTime) {
                map.put(key, ((LocalDateTime) value).format(sdf));
            }
        }
    }
}
